using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using ECommerce.Library.ECommerce;
using Entity;
using ECommerce.Library.Web;
using System.Data.SqlTypes;
using Entity;

namespace AutoGear.BLL
{
    public class ProductPurchaseOrder : IPurchaseOrder
    {
        private ShoppingCart _shoppingCart;
        private ProductShipping _productShipping;
        private StateTax _stateTax;
        private ICreditCard _card;
        private int _giftCertificateId;
        private double _creditCardAmount = 0;
        private double _giftCertificateAmount = 0;
        private IAddress _billingAddress;
        private IAddress _shippingAddress;
        private ShippingStatus _shippingStatus;
        private PaymentType _paymentType;
        private int _orderId;
        private DateTime _orderDate;
        private OrderStatus _orderStatus;
        private Coupon _coupon;
        private double _taxAmount;
        private double _couponDiscount;
        private double _orderPrice;
        private string _orderNumber = string.Empty;
        private string _orderTrackNumber = string.Empty;
        private string _specialInstruction = string.Empty;
        private string _orderStatusMessage = string.Empty;
        private string _orderStatusCode = string.Empty;
        private string _giftCardImageUrl = string.Empty;
        private string _gIftCardMessage = string.Empty;
        private string _officeUse = string.Empty;
        private bool _isMultipleShip = false;
        private bool _isResidential = false;
        private DateTime _requestedDeliveryDate;
        private int _printNo;
        private string _substitute;
        private int _sPId;
        private string _sPName;

        public int OrderId
        {
            get { return _orderId; }
        }

        public string OrderNumber
        {
            get { return _orderNumber; }
        }

        public decimal OrderAmount
        {
            get { return Convert.ToDecimal(_orderPrice); }
        }
        public double TaxAmount
        {
            get
            {
                try
                {
                    //if (this._stateTax != null && !string.IsNullOrEmpty(this._stateTax.StateName))
                    if (this._stateTax != null)
                    {
                        _taxAmount = this._shoppingCart.SubTotal * this._stateTax.Tax / 100;
                    }
                }
                catch { }
                return _taxAmount;
            }

        }

        public DateTime OrderDate
        {
            get { return _orderDate; }
            set { this._orderDate = value; }
        }

        public ShoppingCart ShoppingCart
        {
            get { return _shoppingCart; }
            set { _shoppingCart = value; }
        }

        public ProductShipping ProductShipping
        {
            get { return _productShipping; }
            set { _productShipping = value; }
        }

        public StateTax ProductTax
        {
            get { return _stateTax; }
            set { _stateTax = value; }
        }

        public ShippingStatus ShippingStatus
        {
            get { return _shippingStatus; }
            set { _shippingStatus = value; }
        }

        public PaymentType PaymentType
        {
            get { return _paymentType; }
            set { _paymentType = value; }
        }

        public OrderStatus OrderStatus
        {
            get { return _orderStatus; }
            set { _orderStatus = value; }
        }

        public Coupon Coupon
        {
            get { return _coupon; }
            set { _coupon = value; }
        }
        public string OrderStatusMessage
        {
            get { return _orderStatusMessage; }
            set { _orderStatusMessage = value; }
        }

        public string OrderStatusCode
        {
            get { return _orderStatusCode; }
            set { _orderStatusCode = value; }
        }

        public string OrderTrackNumber
        {
            get { return _orderTrackNumber; }
            set { _orderTrackNumber = value; }
        }

        public string SpecialInstruction
        {
            get { return _specialInstruction; }
            set { _specialInstruction = value; }
        }

        public string GIftCardMessage 
        {
            get { return _gIftCardMessage; }
            set { _gIftCardMessage = value; }
        }
        public string GiftCardImageUrl 
        {
            get { return _giftCardImageUrl; }
            set { _giftCardImageUrl = value; }
        
        }
        public int GiftCertificateID
        {
            get { return _giftCertificateId; }
            set { _giftCertificateId = value; }
        }
        public double CreditCardAmount
        {
            get
            {
                return this._creditCardAmount;
            }
            set
            {
                this._creditCardAmount = value;
            }
        }
        public double GiftCertificateAmount
        {
            get
            {
                return this._giftCertificateAmount;
            }
            set
            {
                this._giftCertificateAmount = value;
            }
        }

        public bool IsMultipleShip
        {
            get { return _isMultipleShip; }
            set { _isMultipleShip = value; }
        }
        public bool IsResidential
        {
            get { return _isResidential; }
            set { _isResidential = value; }
        }
        public string OfficeUse 
        {
            get { return this._officeUse; }
            set { this._officeUse = value; }
        }
        public DateTime RequestedDeliveryDate
        {
            get { return _requestedDeliveryDate; }
            set { this._requestedDeliveryDate = value; }
        }

        public int PrintNo
        {
            get { return _printNo; }
            set { _printNo = value; }
        }

        public string Substitute 
        {
            get { return _substitute; }
            set { _substitute = value; }
        }
        public int SPId 
        {
            get { return _sPId; }
            set { _sPId = value; }
        }

        public string SPName 
        {
            get { return _sPName; }
            set { _sPName = value; }
        }

        #region IPurchaseOrder Members

        public ICreditCard CreditCard
        {
            get { return this._card; }
            set { this._card = value; }
        }

        public IAddress BillingAddress
        {
            get { return this._billingAddress; }
            set { this._billingAddress = value; }
        }

        public IAddress ShippingAddress
        {
            get { return this._shippingAddress; }
            set { this._shippingAddress = value; }
        }

        public double TotalAmount
        {
            get
            {
                //double subTotal = (this._shoppingCart.SubTotal + this._shoppingCart.GiftSubTotal) - this.CouponDiscount < 0 ? 0.0 : (this._shoppingCart.SubTotal + this._shoppingCart.GiftSubTotal)  - this.CouponDiscount;
                double subTotal = (this._shoppingCart.SubTotal + this._shoppingCart.GiftSubTotal) - this.CouponDiscount < 0 ? 0.0 : (this._shoppingCart.SubTotal + this._shoppingCart.GiftSubTotal) - this.CouponDiscount;
                this._orderPrice = subTotal + this.TaxAmount + this._productShipping.ProductShippingCost;
                return this._orderPrice;
            }
        }

        public double CouponDiscount
        {
            get
            {
                double discount = 0.0;
                if (_coupon != null)
                {
                    if (_coupon.CalculationIn == CouponCalculationIn.Percentage)
                    {
                        discount = (this._shoppingCart.SubTotal * _coupon.CouponAmount) / 100;
                    }
                    else
                    {
                        discount = _coupon.CouponAmount;
                    }
                    _couponDiscount = discount;
                }     
                return _couponDiscount; 
            }
        }

        #endregion


        public ProductPurchaseOrder()
        {

        }

        public ProductPurchaseOrder(string orderNumber, string supplierName)
        {
            TblProductPurchaseOrder tblProductPurchaseOrder = TblProductPurchaseOrderExtended.GetProductPurchaseOrderByOrderCode(orderNumber);
            if (tblProductPurchaseOrder != null)
            {
                ShoppingCart shoppingCart = new ShoppingCart();

                Address billingAddress = new Address(tblProductPurchaseOrder.BillingFirstName, tblProductPurchaseOrder.BillingLastName, tblProductPurchaseOrder.BillingAddressLine1, tblProductPurchaseOrder.BillingAddressLine2, tblProductPurchaseOrder.BillingCity, tblProductPurchaseOrder.BillingAddressState, tblProductPurchaseOrder.BillingAddressCountry, tblProductPurchaseOrder.BillingZipCode, tblProductPurchaseOrder.BillingTelephoneNumber, tblProductPurchaseOrder.BillingTelephoneNumber2, tblProductPurchaseOrder.BillingEmailAddress);
                Address shippingAddress = new Address(tblProductPurchaseOrder.ShippingFirstName, tblProductPurchaseOrder.ShippingLastName, tblProductPurchaseOrder.ShippingAddressLine1, tblProductPurchaseOrder.ShippingAddressLine2, tblProductPurchaseOrder.ShippingCity, tblProductPurchaseOrder.ShippingAddressState, tblProductPurchaseOrder.ShippingAddressCountry, tblProductPurchaseOrder.ShippingZipCode, tblProductPurchaseOrder.ShippingTelephoneNumber, string.Empty, tblProductPurchaseOrder.ShippingEmailAddress);
                CreditCard card = null;
                if (tblProductPurchaseOrder.CreditCardId != null && tblProductPurchaseOrder.CreditCardId > 0)
                {
                    TblCreditCard tblCreditCard = TblCreditCard.GetTblCreditCardByCardId(tblProductPurchaseOrder.CreditCardId);
                    card = new CreditCard(tblCreditCard.CardId, tblCreditCard.CardType, tblCreditCard.CardNumber, tblCreditCard.CardCode, tblCreditCard.ExpireDate);
                }

                ECommerce.DAL.CustomCollection<TblProductPurchaseOrderItems> tblOrderItems = TblProductPurchaseOrderItemsExtended.GetOrderItemsByOrderId(tblProductPurchaseOrder.OrderId);
                for (int i = 0; i < tblOrderItems.Count; i++)
                {
                    TblProductPurchaseOrderItems orderItem = tblOrderItems[i];
                    shoppingCart.AddCartItem(new CartItem(Product.Load(orderItem.ProductId,supplierName), orderItem.Quantity, orderItem.MetalType, orderItem.GiftWrapper,true));
                }
                ProductShipping productShipping = new ProductShipping(tblProductPurchaseOrder.ShippingMethod, 0.0, Convert.ToDouble(tblProductPurchaseOrder.ShippingCost));

                this._shoppingCart = shoppingCart;
                this._productShipping = productShipping;

                //this._stateTax = new StateTax();
                //this._stateTax.StateName = tblProductPurchaseOrder.BillingAddressState;
                //this._stateTax.StateCode= SiteUtility.GetStateCodeFromStateName( tblProductPurchaseOrder.BillingAddressState);
                //double taxPercentage;
                //if (tblProductPurchaseOrder.TaxAmount <= 0)
                //{
                //    taxPercentage = Convert.ToDouble(((tblProductPurchaseOrder.OrderPrice - tblProductPurchaseOrder.TaxAmount) * 100));
                //}
                //else
                //{ 
                //    //taxPercentage = Convert.ToDouble(((tblProductPurchaseOrder.OrderPrice - tblProductPurchaseOrder.TaxAmount) * 100) / tblProductPurchaseOrder.TaxAmount); 
                //    taxPercentage =  Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
                //}

                ////this._stateTax.Tax = taxPercentage;
                //this._stateTax.Tax= Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
                this._taxAmount = Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);

                this._billingAddress = billingAddress;
                this._shippingAddress = shippingAddress;
                if (card != null)
                {
                    this._card = card;
                    this._creditCardAmount = Convert.ToDouble(tblProductPurchaseOrder.PaymentByCreditCard);
                }
                if (tblProductPurchaseOrder.GiftCertificateId != null && tblProductPurchaseOrder.GiftCertificateId > 0)
                {
                    this._giftCertificateId = tblProductPurchaseOrder.GiftCertificateId;
                    this._giftCertificateAmount = Convert.ToDouble(tblProductPurchaseOrder.PaymentByGiftCertificate);
                }
                this._orderId = tblProductPurchaseOrder.OrderId;
                this._orderNumber = tblProductPurchaseOrder.OrderCode;
                this._orderDate = tblProductPurchaseOrder.CreateDate;
                this._orderPrice = Convert.ToDouble(tblProductPurchaseOrder.OrderPrice);
                this._orderTrackNumber = tblProductPurchaseOrder.OrderTrackNumber;
                this._shippingStatus = (ShippingStatus)Enum.Parse(typeof(ShippingStatus), tblProductPurchaseOrder.ShippingStatus, true);
                this._paymentType = (PaymentType)Enum.Parse(typeof(PaymentType), tblProductPurchaseOrder.PaymentType, true);
                this._orderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), tblProductPurchaseOrder.OrderStatus, true);
                this._specialInstruction = tblProductPurchaseOrder.SpecialInstruction;
                this._orderStatusMessage = tblProductPurchaseOrder.OrderStatusMessage;
                this._orderStatusCode = tblProductPurchaseOrder.OrderStatusCode;
                this._couponDiscount = Convert.ToDouble(tblProductPurchaseOrder.CouponDiscount);
                this._giftCardImageUrl = tblProductPurchaseOrder.GiftCardImageUrl;
                this._gIftCardMessage = tblProductPurchaseOrder.GIftCardMessage;
                this._officeUse = tblProductPurchaseOrder.OfficeUse;
                this._isMultipleShip = tblProductPurchaseOrder.IsMultipleShip;
                this._isResidential = tblProductPurchaseOrder.IsResidential;
                this._requestedDeliveryDate = tblProductPurchaseOrder.RequestedDeliveryDate;
                this._printNo = tblProductPurchaseOrder.PrintNo;
                this._substitute = tblProductPurchaseOrder.Substitute;
                this._sPId = tblProductPurchaseOrder.SPId;
                this._sPName = tblProductPurchaseOrder.SPName;
            }
        }

        public ProductPurchaseOrder(string orderNumber)
        {
            TblProductPurchaseOrder tblProductPurchaseOrder = TblProductPurchaseOrderExtended.GetProductPurchaseOrderByOrderCode(orderNumber);
            if (tblProductPurchaseOrder != null)
            {
                ShoppingCart shoppingCart = new ShoppingCart();

                Address billingAddress = new Address(tblProductPurchaseOrder.BillingFirstName, tblProductPurchaseOrder.BillingLastName, tblProductPurchaseOrder.BillingAddressLine1, tblProductPurchaseOrder.BillingAddressLine2, tblProductPurchaseOrder.BillingCity, tblProductPurchaseOrder.BillingAddressState, tblProductPurchaseOrder.BillingAddressCountry, tblProductPurchaseOrder.BillingZipCode, tblProductPurchaseOrder.BillingTelephoneNumber, tblProductPurchaseOrder.BillingTelephoneNumber2, tblProductPurchaseOrder.BillingEmailAddress);
                Address shippingAddress = new Address(tblProductPurchaseOrder.ShippingFirstName, tblProductPurchaseOrder.ShippingLastName, tblProductPurchaseOrder.ShippingAddressLine1, tblProductPurchaseOrder.ShippingAddressLine2, tblProductPurchaseOrder.ShippingCity, tblProductPurchaseOrder.ShippingAddressState, tblProductPurchaseOrder.ShippingAddressCountry, tblProductPurchaseOrder.ShippingZipCode, tblProductPurchaseOrder.ShippingTelephoneNumber,string.Empty, tblProductPurchaseOrder.ShippingEmailAddress);
                CreditCard card = null;
                if (tblProductPurchaseOrder.CreditCardId != null && tblProductPurchaseOrder.CreditCardId > 0)
                {
                    TblCreditCard tblCreditCard = TblCreditCard.GetTblCreditCardByCardId(tblProductPurchaseOrder.CreditCardId);
                    card = new CreditCard(tblCreditCard.CardId,tblCreditCard.CardType, tblCreditCard.CardNumber, tblCreditCard.CardCode, tblCreditCard.ExpireDate);
                }

                ECommerce.DAL.CustomCollection<TblProductPurchaseOrderItems> tblOrderItems = TblProductPurchaseOrderItemsExtended.GetOrderItemsByOrderId(tblProductPurchaseOrder.OrderId);
                for (int i = 0; i < tblOrderItems.Count; i++)
                {
                    TblProductPurchaseOrderItems orderItem = tblOrderItems[i];
                    shoppingCart.AddCartItem(new CartItem(Product.Load(orderItem.ProductId), orderItem.Quantity, (double)orderItem.Price, orderItem.MetalType, orderItem.GiftWrapper));
                }
                ProductShipping productShipping = new ProductShipping(tblProductPurchaseOrder.ShippingMethod, 0.0, Convert.ToDouble(tblProductPurchaseOrder.ShippingCost));

                this._shoppingCart = shoppingCart;
                this._productShipping = productShipping;

                //this._stateTax = new StateTax();
                //this._stateTax.StateName = tblProductPurchaseOrder.BillingAddressState;
                //this._stateTax.StateCode= SiteUtility.GetStateCodeFromStateName( tblProductPurchaseOrder.BillingAddressState);
                //double taxPercentage;
                //if (tblProductPurchaseOrder.TaxAmount <= 0)
                //{
                //    taxPercentage = Convert.ToDouble(((tblProductPurchaseOrder.OrderPrice - tblProductPurchaseOrder.TaxAmount) * 100));
                //}
                //else
                //{ 
                //    //taxPercentage = Convert.ToDouble(((tblProductPurchaseOrder.OrderPrice - tblProductPurchaseOrder.TaxAmount) * 100) / tblProductPurchaseOrder.TaxAmount); 
                //    taxPercentage =  Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
                //}
                
                ////this._stateTax.Tax = taxPercentage;
                //this._stateTax.Tax= Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
                this._taxAmount = Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);

                this._billingAddress = billingAddress;
                this._shippingAddress = shippingAddress;
                if (card != null)
                {
                    this._card = card;
                    this._creditCardAmount = Convert.ToDouble(tblProductPurchaseOrder.PaymentByCreditCard);
                }
                if (tblProductPurchaseOrder.GiftCertificateId != null && tblProductPurchaseOrder.GiftCertificateId>0)
                {
                    this._giftCertificateId = tblProductPurchaseOrder.GiftCertificateId;
                    this._giftCertificateAmount = Convert.ToDouble(tblProductPurchaseOrder.PaymentByGiftCertificate);
                }
                this._orderId = tblProductPurchaseOrder.OrderId;
                this._orderNumber = tblProductPurchaseOrder.OrderCode;
                this._orderDate = tblProductPurchaseOrder.CreateDate;
                this._orderPrice = Convert.ToDouble(tblProductPurchaseOrder.OrderPrice);
                this._orderTrackNumber = tblProductPurchaseOrder.OrderTrackNumber;
                this._shippingStatus = (ShippingStatus)Enum.Parse(typeof(ShippingStatus), tblProductPurchaseOrder.ShippingStatus, true);
                this._paymentType = (PaymentType)Enum.Parse(typeof(PaymentType), tblProductPurchaseOrder.PaymentType, true);
                this._orderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), tblProductPurchaseOrder.OrderStatus, true);
                this._specialInstruction = tblProductPurchaseOrder.SpecialInstruction;
                this._orderStatusMessage = tblProductPurchaseOrder.OrderStatusMessage;
                this._orderStatusCode = tblProductPurchaseOrder.OrderStatusCode;
                this._couponDiscount = Convert.ToDouble(tblProductPurchaseOrder.CouponDiscount);
                this._giftCardImageUrl = tblProductPurchaseOrder.GiftCardImageUrl;
                this._gIftCardMessage = tblProductPurchaseOrder.GIftCardMessage;
                this._officeUse = tblProductPurchaseOrder.OfficeUse;
                this._isMultipleShip=tblProductPurchaseOrder.IsMultipleShip;
                this._isResidential = tblProductPurchaseOrder.IsResidential;
                this._requestedDeliveryDate = tblProductPurchaseOrder.RequestedDeliveryDate;
                this._printNo = tblProductPurchaseOrder.PrintNo;
                this._substitute = tblProductPurchaseOrder.Substitute;
                this._sPId = tblProductPurchaseOrder.SPId;
                this._sPName = tblProductPurchaseOrder.SPName;
            }
        }

        public ProductPurchaseOrder(string orderNumber, bool isActive)
        {
            TblProductPurchaseOrder tblProductPurchaseOrder = TblProductPurchaseOrderExtended.GetProductPurchaseOrderByOrderCode(orderNumber);
            if (tblProductPurchaseOrder != null)
            {
                ShoppingCart shoppingCart = new ShoppingCart();

                Address billingAddress = new Address(tblProductPurchaseOrder.BillingFirstName, tblProductPurchaseOrder.BillingLastName, tblProductPurchaseOrder.BillingAddressLine1, tblProductPurchaseOrder.BillingAddressLine2, tblProductPurchaseOrder.BillingCity, tblProductPurchaseOrder.BillingAddressState, tblProductPurchaseOrder.BillingAddressCountry, tblProductPurchaseOrder.BillingZipCode, tblProductPurchaseOrder.BillingTelephoneNumber, tblProductPurchaseOrder.BillingTelephoneNumber2, tblProductPurchaseOrder.BillingEmailAddress);
                Address shippingAddress = new Address(tblProductPurchaseOrder.ShippingFirstName, tblProductPurchaseOrder.ShippingLastName, tblProductPurchaseOrder.ShippingAddressLine1, tblProductPurchaseOrder.ShippingAddressLine2, tblProductPurchaseOrder.ShippingCity, tblProductPurchaseOrder.ShippingAddressState, tblProductPurchaseOrder.ShippingAddressCountry, tblProductPurchaseOrder.ShippingZipCode, tblProductPurchaseOrder.ShippingTelephoneNumber, string.Empty, tblProductPurchaseOrder.ShippingEmailAddress);
                CreditCard card = null;
                if (tblProductPurchaseOrder.CreditCardId != null && tblProductPurchaseOrder.CreditCardId > 0)
                {
                    TblCreditCard tblCreditCard = TblCreditCard.GetTblCreditCardByCardId(tblProductPurchaseOrder.CreditCardId);
                    card = new CreditCard(tblCreditCard.CardId, tblCreditCard.CardType, tblCreditCard.CardNumber, tblCreditCard.CardCode, tblCreditCard.ExpireDate);
                }

                ECommerce.DAL.CustomCollection<TblProductPurchaseOrderItems> tblOrderItems = TblProductPurchaseOrderItemsExtended.GetActiveProductOrderItemsByOrderId(tblProductPurchaseOrder.OrderId);
                for (int i = 0; i < tblOrderItems.Count; i++)
                {
                    TblProductPurchaseOrderItems orderItem = tblOrderItems[i];
                    shoppingCart.AddCartItem(new CartItem(Product.Load(orderItem.ProductId), orderItem.Quantity, (double)orderItem.Price, orderItem.MetalType, orderItem.GiftWrapper));
                }
                ProductShipping productShipping = new ProductShipping(tblProductPurchaseOrder.ShippingMethod, 0.0, Convert.ToDouble(tblProductPurchaseOrder.ShippingCost));

                this._shoppingCart = shoppingCart;
                this._productShipping = productShipping;

                //this._stateTax = new StateTax();
                //this._stateTax.StateName = tblProductPurchaseOrder.BillingAddressState;
                //this._stateTax.StateCode= SiteUtility.GetStateCodeFromStateName( tblProductPurchaseOrder.BillingAddressState);
                //double taxPercentage;
                //if (tblProductPurchaseOrder.TaxAmount <= 0)
                //{
                //    taxPercentage = Convert.ToDouble(((tblProductPurchaseOrder.OrderPrice - tblProductPurchaseOrder.TaxAmount) * 100));
                //}
                //else
                //{ 
                //    //taxPercentage = Convert.ToDouble(((tblProductPurchaseOrder.OrderPrice - tblProductPurchaseOrder.TaxAmount) * 100) / tblProductPurchaseOrder.TaxAmount); 
                //    taxPercentage =  Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
                //}

                ////this._stateTax.Tax = taxPercentage;
                //this._stateTax.Tax= Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
                this._taxAmount = Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);

                this._billingAddress = billingAddress;
                this._shippingAddress = shippingAddress;
                if (card != null)
                {
                    this._card = card;
                    this._creditCardAmount = Convert.ToDouble(tblProductPurchaseOrder.PaymentByCreditCard);
                }
                if (tblProductPurchaseOrder.GiftCertificateId != null && tblProductPurchaseOrder.GiftCertificateId>0)
                {
                    this._giftCertificateId = tblProductPurchaseOrder.GiftCertificateId;
                    this._giftCertificateAmount = Convert.ToDouble(tblProductPurchaseOrder.PaymentByGiftCertificate);
                }
                this._orderId = tblProductPurchaseOrder.OrderId;
                this._orderNumber = tblProductPurchaseOrder.OrderCode;
                this._orderDate = tblProductPurchaseOrder.CreateDate;
                this._orderPrice = Convert.ToDouble(tblProductPurchaseOrder.OrderPrice);
                this._orderTrackNumber = tblProductPurchaseOrder.OrderTrackNumber;
                this._shippingStatus = (ShippingStatus)Enum.Parse(typeof(ShippingStatus), tblProductPurchaseOrder.ShippingStatus, true);
                this._paymentType = (PaymentType)Enum.Parse(typeof(PaymentType), tblProductPurchaseOrder.PaymentType, true);
                this._orderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), tblProductPurchaseOrder.OrderStatus, true);
                this._specialInstruction = tblProductPurchaseOrder.SpecialInstruction;
                this._orderStatusMessage = tblProductPurchaseOrder.OrderStatusMessage;
                this._orderStatusCode = tblProductPurchaseOrder.OrderStatusCode;
                this._couponDiscount = Convert.ToDouble(tblProductPurchaseOrder.CouponDiscount);
                this._giftCardImageUrl = tblProductPurchaseOrder.GiftCardImageUrl;
                this._gIftCardMessage = tblProductPurchaseOrder.GIftCardMessage;
                this._officeUse = tblProductPurchaseOrder.OfficeUse;
                this._isMultipleShip = tblProductPurchaseOrder.IsMultipleShip;
                this._isResidential = tblProductPurchaseOrder.IsResidential;
                this._requestedDeliveryDate = tblProductPurchaseOrder.RequestedDeliveryDate;
                this._printNo = tblProductPurchaseOrder.PrintNo;
                this._substitute = tblProductPurchaseOrder.Substitute;
                this._sPId = tblProductPurchaseOrder.SPId;
                this._sPName = tblProductPurchaseOrder.SPName;
            }
        }



        public ProductPurchaseOrder(ShoppingCart shoppingCart, ProductShipping productShipping, StateTax stateTax,
            Address billingAddress, Address shippingAddress, CreditCard card, Coupon coupon, string giftImageUrl, string giftMessage, string officeUse, string requestedDeliveryDate, int printNo, string substitute,int spId,string sPName)
        {
            this._shoppingCart = shoppingCart;
            this._productShipping = productShipping;
            this._stateTax = stateTax;
            this._billingAddress = billingAddress;
            this._shippingAddress = shippingAddress;
            this._card = card;
            this._shippingStatus = ShippingStatus.NotShipped;
            this._paymentType = PaymentType.CreditCard;
            this._orderId = 0;
            this._orderDate = DateTime.Now;
            this._orderStatus = OrderStatus.CCFail;
            this._orderNumber = string.Empty;
            this._orderTrackNumber = string.Empty;
            this._specialInstruction = string.Empty;
            this._orderStatusMessage = string.Empty;
            this._orderStatusCode = string.Empty;
            this._coupon = coupon;
            this._giftCardImageUrl = giftImageUrl;
            this._gIftCardMessage = giftMessage;
            this._officeUse = officeUse;
            if(! string.IsNullOrEmpty(requestedDeliveryDate))
            this._requestedDeliveryDate =Convert.ToDateTime(requestedDeliveryDate);
            this._printNo = printNo;
            this._substitute = substitute;
            this._sPId = spId;
            this._sPName = sPName;
        }

        private ProductPurchaseOrder(TblProductPurchaseOrder tblProductPurchaseOrder)
        {
            ShoppingCart shoppingCart = new ShoppingCart();
            Address billingAddress = new Address(tblProductPurchaseOrder.BillingFirstName, tblProductPurchaseOrder.BillingLastName, tblProductPurchaseOrder.BillingAddressLine1, tblProductPurchaseOrder.BillingAddressLine2, tblProductPurchaseOrder.BillingCity, tblProductPurchaseOrder.BillingAddressState, tblProductPurchaseOrder.BillingAddressCountry, tblProductPurchaseOrder.BillingZipCode, tblProductPurchaseOrder.BillingTelephoneNumber, tblProductPurchaseOrder.BillingTelephoneNumber2, tblProductPurchaseOrder.BillingEmailAddress);
            Address shippingAddress = new Address(tblProductPurchaseOrder.ShippingFirstName, tblProductPurchaseOrder.ShippingLastName, tblProductPurchaseOrder.ShippingAddressLine1, tblProductPurchaseOrder.ShippingAddressLine2, tblProductPurchaseOrder.ShippingCity, tblProductPurchaseOrder.ShippingAddressState, tblProductPurchaseOrder.ShippingAddressCountry, tblProductPurchaseOrder.ShippingZipCode, tblProductPurchaseOrder.ShippingTelephoneNumber, string.Empty, tblProductPurchaseOrder.ShippingEmailAddress);
            if (tblProductPurchaseOrder.PaymentType == PaymentType.CreditCard.ToString())
            {
                if (tblProductPurchaseOrder.CreditCardId != 0)
                {
                    TblCreditCard tblCreditCard = TblCreditCard.GetTblCreditCardByCardId(tblProductPurchaseOrder.CreditCardId);
                    CreditCard card = new CreditCard(tblCreditCard.CardId,tblCreditCard.CardType, tblCreditCard.CardNumber, tblCreditCard.CardCode, tblCreditCard.ExpireDate);
                    this._card = card;
                    this._card.CardNumber=Imgalib.Utility.Cryptography.DecryptStringAES(card.CardNumber);
                }
            }
            ECommerce.DAL.CustomCollection<TblProductPurchaseOrderItems> tblOrderItems = TblProductPurchaseOrderItemsExtended.GetOrderItemsByOrderId(tblProductPurchaseOrder.OrderId);
            for (int i = 0; i < tblOrderItems.Count; i++)
            {
                TblProductPurchaseOrderItems orderItem = tblOrderItems[i];
                Product product = Product.Load(orderItem.ProductId);
                if (product != null)
                {
                    if ((double)orderItem.Price == product.ProductPrice)
                    {
                        shoppingCart.AddCartItem(new CartItem(product, orderItem.Quantity, (double)product.ProductPrice, orderItem.MetalType, orderItem.GiftWrapper));
                    }
                    else 
                    {
                        shoppingCart.AddCartItem(new CartItem(product, orderItem.Quantity, (double)orderItem.Price, orderItem.MetalType, orderItem.GiftWrapper));
                    }
                }
            }

            ProductShipping productShipping = new ProductShipping(tblProductPurchaseOrder.ShippingMethod, 0.0, Convert.ToDouble(tblProductPurchaseOrder.ShippingCost));

            this._shoppingCart = shoppingCart;
            this._productShipping = productShipping;
            this._taxAmount = Convert.ToDouble(tblProductPurchaseOrder.TaxAmount);
            this._billingAddress = billingAddress;
            this._shippingAddress = shippingAddress;
            
            this._orderId = tblProductPurchaseOrder.OrderId;
            this._orderNumber = tblProductPurchaseOrder.OrderCode;
            this._orderDate = tblProductPurchaseOrder.CreateDate;
            this._orderPrice = Convert.ToDouble(tblProductPurchaseOrder.OrderPrice);
            this._shippingStatus = tblProductPurchaseOrder.ShippingStatus.ToString() == ShippingStatus.NotShipped.ToString() ? ShippingStatus.NotShipped : ShippingStatus.Shipped;
            this._orderTrackNumber = tblProductPurchaseOrder.OrderTrackNumber;
            this._paymentType = tblProductPurchaseOrder.PaymentType.ToString() == PaymentType.CreditCard.ToString() ? PaymentType.CreditCard : PaymentType.GiftCoupon;
            this._orderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), tblProductPurchaseOrder.OrderStatus);
            this._specialInstruction = tblProductPurchaseOrder.SpecialInstruction;
            this._orderStatusMessage = tblProductPurchaseOrder.OrderStatusMessage;
            this._orderStatusCode = tblProductPurchaseOrder.OrderStatusCode;
            this._couponDiscount = Convert.ToDouble(tblProductPurchaseOrder.CouponDiscount);
            this._giftCardImageUrl = tblProductPurchaseOrder.GiftCardImageUrl;
            this._gIftCardMessage = tblProductPurchaseOrder.GIftCardMessage;
            this._officeUse = tblProductPurchaseOrder.OfficeUse;
            this.IsMultipleShip = tblProductPurchaseOrder.IsMultipleShip;
            this._isResidential = tblProductPurchaseOrder.IsResidential;
            this._requestedDeliveryDate = tblProductPurchaseOrder.RequestedDeliveryDate;
            this._printNo = tblProductPurchaseOrder.PrintNo;
            this._substitute = tblProductPurchaseOrder.Substitute;
            this._sPId = tblProductPurchaseOrder.SPId;
            this._sPName = tblProductPurchaseOrder.SPName;
        }

        public static ProductPurchaseOrder Load(int orderId)
        {
            TblProductPurchaseOrder tblProductPurchaseOrder = TblProductPurchaseOrder.GetTblProductPurchaseOrderByOrderId(orderId);
            return new ProductPurchaseOrder(tblProductPurchaseOrder);
        }

        public void CreatePartnerOrder()
        {
            CustomerUser customerUser = new CustomerUser();

            TblProductPurchaseOrder tblProductPurchaseOrder = new TblProductPurchaseOrder();

            if (SessionManager.IsUserLoggedIn)
            {
                SiteUser siteUser = SessionManager.LoggedUser;
                if (siteUser is CustomerUser)
                {
                    customerUser = siteUser as CustomerUser;
                    if (customerUser != null)
                    {
                        UserProfile profile = customerUser.Profile;
                        tblProductPurchaseOrder.FirstName = profile.FirstName;
                        tblProductPurchaseOrder.LastName = profile.LastName;
                        tblProductPurchaseOrder.DateOfBirth = profile.DateOfBirth;
                        tblProductPurchaseOrder.EmailAddress = profile.EmailAddress;
                        tblProductPurchaseOrder.UserGender = profile.UserGender.ToString();
                        tblProductPurchaseOrder.UserTelephoneNumber = profile.UserTelephoneNumber;
                        tblProductPurchaseOrder.UserFaxNumber = profile.UserFaxNumber;
                        tblProductPurchaseOrder.CompanyName = profile.CompanyName;
                        tblProductPurchaseOrder.CompanyTelephoneNumber = profile.CompanyTelephoneNumber;
                        tblProductPurchaseOrder.CompanyFaxNumber = profile.CompanyFaxNumber;
                    }
                }
            }

            if (tblProductPurchaseOrder.DateOfBirth < SqlDateTime.MinValue.Value)
                tblProductPurchaseOrder.DateOfBirth = SqlDateTime.MinValue.Value;

            tblProductPurchaseOrder.BillingFirstName = this._billingAddress.FirstName;
            tblProductPurchaseOrder.BillingLastName = this._billingAddress.LastName;
            tblProductPurchaseOrder.BillingAddressLine1 = this._billingAddress.AddressLine1;
            tblProductPurchaseOrder.BillingAddressLine2 = this._billingAddress.AddressLine2;
            tblProductPurchaseOrder.BillingAddressState = this._billingAddress.AddressState;
            tblProductPurchaseOrder.BillingCity = this._billingAddress.City;
            tblProductPurchaseOrder.BillingAddressCountry = this._billingAddress.AddressCountry;
            tblProductPurchaseOrder.BillingZipCode = this._billingAddress.ZipCode;
            tblProductPurchaseOrder.BillingTelephoneNumber = this._billingAddress.TelephoneNumber;
            tblProductPurchaseOrder.BillingTelephoneNumber2 = this._billingAddress.TelephoneNumber2;
            tblProductPurchaseOrder.BillingEmailAddress = this._billingAddress.EmailAddress;

            tblProductPurchaseOrder.ShippingFirstName = this._shippingAddress.FirstName;
            tblProductPurchaseOrder.ShippingLastName = this._shippingAddress.LastName;
            tblProductPurchaseOrder.ShippingAddressLine1 = this._shippingAddress.AddressLine1;
            tblProductPurchaseOrder.ShippingAddressLine2 = this._shippingAddress.AddressLine2;
            tblProductPurchaseOrder.ShippingAddressState = this._shippingAddress.AddressState;
            tblProductPurchaseOrder.ShippingCity = this._shippingAddress.City;
            tblProductPurchaseOrder.ShippingAddressCountry = this._shippingAddress.AddressCountry;
            tblProductPurchaseOrder.ShippingZipCode = this._shippingAddress.ZipCode;
            tblProductPurchaseOrder.ShippingTelephoneNumber = this._shippingAddress.TelephoneNumber;
            tblProductPurchaseOrder.ShippingEmailAddress = this._shippingAddress.EmailAddress;
            tblProductPurchaseOrder.OrderStatusMessage = this._orderStatusMessage;
            tblProductPurchaseOrder.OrderStatusCode = this._orderStatusCode;
            tblProductPurchaseOrder.CreateDate = DateTime.Now;
            tblProductPurchaseOrder.IsMultipleShip = this.IsMultipleShip;
            tblProductPurchaseOrder.IsResidential = this.IsResidential;
            if (this.RequestedDeliveryDate > DateTime.Now)
            {
                tblProductPurchaseOrder.RequestedDeliveryDate = this.RequestedDeliveryDate;
            }
            else
            {
                tblProductPurchaseOrder.RequestedDeliveryDate = DateTime.Now;
            }
            tblProductPurchaseOrder.PrintNo = this.PrintNo;
            tblProductPurchaseOrder.Substitute = this.Substitute;
            tblProductPurchaseOrder.SPId = this.SPId;
            tblProductPurchaseOrder.SPName = this.SPName;
            CreditCard card = (CreditCard)this._card;
            if (card != null)
            {
                tblProductPurchaseOrder.CreditCardId = card.CardId;
                tblProductPurchaseOrder.PaymentByCreditCard = Convert.ToDecimal(this._creditCardAmount);
            }

            if (this._giftCertificateId > 0)
            {
                tblProductPurchaseOrder.GiftCertificateId = this._giftCertificateId;
                tblProductPurchaseOrder.PaymentByGiftCertificate = Convert.ToDecimal(this._giftCertificateAmount);
            }

            tblProductPurchaseOrder.PaymentType = this._paymentType.ToString();
            tblProductPurchaseOrder.OrderPrice = Convert.ToDecimal(this.TotalAmount);
            tblProductPurchaseOrder.ShippingStatus = this._shippingStatus.ToString();
            tblProductPurchaseOrder.TaxAmount = Convert.ToDecimal(this._taxAmount);
            if (this.ProductShipping.ProductShippingMethod != null)
            {
                tblProductPurchaseOrder.ShippingMethod = this.ProductShipping.ProductShippingMethod;
                tblProductPurchaseOrder.ShippingCost = Convert.ToDecimal(this._productShipping.ProductShippingCost);
                tblProductPurchaseOrder.ShippingStatus = this._shippingStatus.ToString();
            }
            tblProductPurchaseOrder.OrderStatus = this._orderStatus.ToString();
            tblProductPurchaseOrder.OrderTrackNumber = this._orderTrackNumber;
            tblProductPurchaseOrder.SpecialInstruction = this._specialInstruction;
            tblProductPurchaseOrder.CouponDiscount = Convert.ToDecimal(this._couponDiscount);
            tblProductPurchaseOrder.GiftCardImageUrl = this._giftCardImageUrl;
            tblProductPurchaseOrder.GIftCardMessage = this._gIftCardMessage;
            tblProductPurchaseOrder.OfficeUse = this._officeUse;
            tblProductPurchaseOrder.Save();

            this._orderId = tblProductPurchaseOrder.OrderId;
            this._orderNumber = SiteSettings.OrderPrefix + this._orderId;
            tblProductPurchaseOrder.OrderCode = this._orderNumber;
            TblProductPurchaseOrder.UpdateTblProductPurchaseOrder(tblProductPurchaseOrder);

            foreach (CartItem items in this._shoppingCart.CartItems)
            {
                TblProductPurchaseOrderItems orderItem = new TblProductPurchaseOrderItems();
                orderItem.OrderId = tblProductPurchaseOrder.OrderId;
                orderItem.ProductId = items.Product.ProductId;
                orderItem.Quantity = items.Quantity;
                orderItem.MetalType = items.MetalType;
                orderItem.GiftWrapper = items.GiftWrapper;
                orderItem.Price = Convert.ToDecimal(items.Product.ProductPrice);
                orderItem.Save();
            }

            if (customerUser != null && customerUser.UserId > 0)
            {
                TblUserOrder userOrder = new TblUserOrder();
                userOrder.UserId = customerUser.UserId;
                userOrder.OrderId = tblProductPurchaseOrder.OrderId;
                userOrder.Save();
            }
        }

        public void Create()
        {
            CustomerUser customerUser = new CustomerUser();

            TblProductPurchaseOrder tblProductPurchaseOrder = new TblProductPurchaseOrder();

            if (SessionManager.IsUserLoggedIn)
            {
                SiteUser siteUser = SessionManager.LoggedUser;
                if (siteUser is CustomerUser)
                {
                    customerUser = siteUser as CustomerUser;
                    if (customerUser != null)
                    {
                        UserProfile profile = customerUser.Profile;
                        tblProductPurchaseOrder.FirstName = profile.FirstName;
                        tblProductPurchaseOrder.LastName = profile.LastName;
                        tblProductPurchaseOrder.DateOfBirth = profile.DateOfBirth;
                        tblProductPurchaseOrder.EmailAddress = profile.EmailAddress;
                        tblProductPurchaseOrder.UserGender = profile.UserGender.ToString();
                        tblProductPurchaseOrder.UserTelephoneNumber = profile.UserTelephoneNumber;
                        tblProductPurchaseOrder.UserFaxNumber = profile.UserFaxNumber;
                        tblProductPurchaseOrder.CompanyName = profile.CompanyName;
                        tblProductPurchaseOrder.CompanyTelephoneNumber = profile.CompanyTelephoneNumber;
                        tblProductPurchaseOrder.CompanyFaxNumber = profile.CompanyFaxNumber;
                    }
                }
            }

            if (tblProductPurchaseOrder.DateOfBirth < SqlDateTime.MinValue.Value)
                tblProductPurchaseOrder.DateOfBirth = SqlDateTime.MinValue.Value;

            tblProductPurchaseOrder.BillingFirstName = this._billingAddress.FirstName;
            tblProductPurchaseOrder.BillingLastName = this._billingAddress.LastName;
            tblProductPurchaseOrder.BillingAddressLine1 = this._billingAddress.AddressLine1;
            tblProductPurchaseOrder.BillingAddressLine2 = this._billingAddress.AddressLine2;
            tblProductPurchaseOrder.BillingAddressState = this._billingAddress.AddressState;
            tblProductPurchaseOrder.BillingCity = this._billingAddress.City;
            tblProductPurchaseOrder.BillingAddressCountry = this._billingAddress.AddressCountry;
            tblProductPurchaseOrder.BillingZipCode = this._billingAddress.ZipCode;
            tblProductPurchaseOrder.BillingTelephoneNumber = this._billingAddress.TelephoneNumber;
            tblProductPurchaseOrder.BillingTelephoneNumber2 = this._billingAddress.TelephoneNumber2;
            tblProductPurchaseOrder.BillingEmailAddress = this._billingAddress.EmailAddress;

            tblProductPurchaseOrder.ShippingFirstName = this._shippingAddress.FirstName;
            tblProductPurchaseOrder.ShippingLastName = this._shippingAddress.LastName;
            tblProductPurchaseOrder.ShippingAddressLine1 = this._shippingAddress.AddressLine1;
            tblProductPurchaseOrder.ShippingAddressLine2 = this._shippingAddress.AddressLine2;
            tblProductPurchaseOrder.ShippingAddressState = this._shippingAddress.AddressState;
            tblProductPurchaseOrder.ShippingCity = this._shippingAddress.City;
            tblProductPurchaseOrder.ShippingAddressCountry = this._shippingAddress.AddressCountry;
            tblProductPurchaseOrder.ShippingZipCode = this._shippingAddress.ZipCode;
            tblProductPurchaseOrder.ShippingTelephoneNumber = this._shippingAddress.TelephoneNumber;
            tblProductPurchaseOrder.ShippingEmailAddress = this._shippingAddress.EmailAddress;
            tblProductPurchaseOrder.OrderStatusMessage = this._orderStatusMessage;
            tblProductPurchaseOrder.OrderStatusCode = this._orderStatusCode;
            tblProductPurchaseOrder.CreateDate = DateTime.Now;
            tblProductPurchaseOrder.IsMultipleShip=this.IsMultipleShip;
            tblProductPurchaseOrder.IsResidential = this.IsResidential;
            if (this.RequestedDeliveryDate > DateTime.Now)
            {
                tblProductPurchaseOrder.RequestedDeliveryDate = this.RequestedDeliveryDate;
            }
            else 
            {
                tblProductPurchaseOrder.RequestedDeliveryDate = DateTime.Now;
            }
            tblProductPurchaseOrder.PrintNo = this.PrintNo;
            tblProductPurchaseOrder.Substitute = this.Substitute;
            tblProductPurchaseOrder.SPId = this.SPId;
            tblProductPurchaseOrder.SPName = this.SPName;
            CreditCard card = (CreditCard)this._card;
            if (card != null)
            {
                tblProductPurchaseOrder.CreditCardId = card.CardId;
                tblProductPurchaseOrder.PaymentByCreditCard = Convert.ToDecimal(this._creditCardAmount);
            }

            if (this._giftCertificateId > 0)
            {
                tblProductPurchaseOrder.GiftCertificateId = this._giftCertificateId;
                tblProductPurchaseOrder.PaymentByGiftCertificate = Convert.ToDecimal(this._giftCertificateAmount);
            }

            tblProductPurchaseOrder.PaymentType = this._paymentType.ToString();
            tblProductPurchaseOrder.OrderPrice = Convert.ToDecimal(this.TotalAmount);
            tblProductPurchaseOrder.ShippingStatus = this._shippingStatus.ToString();
            tblProductPurchaseOrder.TaxAmount = Convert.ToDecimal(this._taxAmount);
            if (this.ProductShipping.ProductShippingMethod != null)
            {
                tblProductPurchaseOrder.ShippingMethod = this.ProductShipping.ProductShippingMethod;
                tblProductPurchaseOrder.ShippingCost = Convert.ToDecimal(this._productShipping.ProductShippingCost);
                tblProductPurchaseOrder.ShippingStatus = this._shippingStatus.ToString();
            }
            tblProductPurchaseOrder.OrderStatus = this._orderStatus.ToString();
            tblProductPurchaseOrder.OrderTrackNumber = this._orderTrackNumber;
            tblProductPurchaseOrder.SpecialInstruction = this._specialInstruction;
            tblProductPurchaseOrder.CouponDiscount = Convert.ToDecimal(this._couponDiscount);
            tblProductPurchaseOrder.GiftCardImageUrl = this._giftCardImageUrl;
            tblProductPurchaseOrder.GIftCardMessage = this._gIftCardMessage;
            tblProductPurchaseOrder.OfficeUse = this._officeUse;
            tblProductPurchaseOrder.Save();

            this._orderId = tblProductPurchaseOrder.OrderId;
            this._orderNumber = SiteSettings.OrderPrefix + this._orderId;
            tblProductPurchaseOrder.OrderCode = this._orderNumber;
            TblProductPurchaseOrder.UpdateTblProductPurchaseOrder(tblProductPurchaseOrder);

            foreach (CartItem items in this._shoppingCart.CartItems)
            {
                TblProductPurchaseOrderItems orderItem = new TblProductPurchaseOrderItems();
                orderItem.OrderId = tblProductPurchaseOrder.OrderId;
                orderItem.ProductId = items.Product.ProductId;
                orderItem.Quantity = items.Quantity;
                orderItem.MetalType = items.MetalType;
                orderItem.GiftWrapper = items.GiftWrapper;
                if (items.Product.StandardCost > 0)
                {
                    orderItem.Price = Convert.ToDecimal(items.Product.StandardCost);
                }
                else
                {
                    orderItem.Price = Convert.ToDecimal(items.Product.ProductPrice);
                }
                orderItem.Save();
            }

            foreach (GiftItem item in this._shoppingCart.GiftItems)
            {
                TblGiftCertificate giftCertificate = new TblGiftCertificate();
                giftCertificate.Code = item.GiftCertificateCode;
                giftCertificate.FromEmailAddress = item.FromEmailAddress;
                giftCertificate.FromName = item.FromName;
                giftCertificate.ToEmailAddress = item.ToEmailAddress;
                giftCertificate.ToName = item.ToName;
                giftCertificate.Message = item.Message;
                giftCertificate.PriceAmount = item.UnitPrice;
                giftCertificate.DateCreated = item.DateCreated;
                giftCertificate.DateExpired = item.DateExpired;
                giftCertificate.Quantity = item.Quantity;
                giftCertificate.CurrentBalance = item.UnitPrice;
                giftCertificate.IsActive = true;

                int newGiftCertificateId = TblGiftCertificate.CreateNewTblGiftCertificate(giftCertificate);

                TblGiftCertificateOrderItems giftCertificateOrderItem = new TblGiftCertificateOrderItems();
                giftCertificateOrderItem.GiftCertificateId = newGiftCertificateId;
                giftCertificateOrderItem.OrderId = tblProductPurchaseOrder.OrderId;

                TblGiftCertificateOrderItems.CreateNewTblGiftCertificateOrderItems(giftCertificateOrderItem);
            }

            if (customerUser != null && customerUser.UserId > 0)
            {
                TblUserOrder userOrder = new TblUserOrder();
                userOrder.UserId = customerUser.UserId;
                userOrder.OrderId = tblProductPurchaseOrder.OrderId;
                userOrder.Save();
            }
        }

        public void Update()
        {

            TblProductPurchaseOrder tblProductPurchaseOrder = TblProductPurchaseOrder.GetTblProductPurchaseOrderByOrderId(OrderId);

            if (this._card != null)
            {
                tblProductPurchaseOrder.CreditCardId = this._card.CardId;
                tblProductPurchaseOrder.PaymentByCreditCard = Convert.ToDecimal(this._creditCardAmount);
            }

            if (this._giftCertificateId != null && tblProductPurchaseOrder.GiftCertificateId > 0)
            {
                tblProductPurchaseOrder.GiftCertificateId = this._giftCertificateId;
                tblProductPurchaseOrder.PaymentByGiftCertificate = Convert.ToDecimal(this._giftCertificateAmount);
            }
            
            tblProductPurchaseOrder.PaymentType = this._paymentType.ToString();
            tblProductPurchaseOrder.OrderTrackNumber = this._orderTrackNumber;
            tblProductPurchaseOrder.OrderStatus = this._orderStatus.ToString();
            tblProductPurchaseOrder.ShippingStatus = this._shippingStatus.ToString();
            tblProductPurchaseOrder.SpecialInstruction = this._specialInstruction;
            tblProductPurchaseOrder.OrderPrice = Convert.ToDecimal(this.TotalAmount);
            tblProductPurchaseOrder.PrintNo = this.PrintNo;
            tblProductPurchaseOrder.Substitute = this._substitute;
            tblProductPurchaseOrder.SPId = this._sPId;
            tblProductPurchaseOrder.SPName = this._sPName;
            tblProductPurchaseOrder.IsMultipleShip = this._isMultipleShip;
            tblProductPurchaseOrder.OrderStatusMessage = this._orderStatusMessage;
            tblProductPurchaseOrder.OrderStatusCode = this._orderStatusCode;
            tblProductPurchaseOrder.OfficeUse = this._officeUse;

            TblProductPurchaseOrder.UpdateTblProductPurchaseOrder(tblProductPurchaseOrder);

        }

        public void Delete()
        {
            TblProductPurchaseOrder.DeleteTblProductPurchaseOrder(this.OrderId);
        }

        public static void DeleteProductPurchaseOrder(List<int> ids)
        {
            TblProductPurchaseOrder.DeleteTblProductPurchaseOrderList(ids);
        }

        public static void DeleteProductPurchaseOrder(ReadWriteCustomCollection<ProductPurchaseOrder> productPurchaseOrders)
        {
            foreach (ProductPurchaseOrder order in productPurchaseOrders)
            {
                order.Delete();
            }
        }

        public static ReadWriteCustomCollection<ProductPurchaseOrder> GetProductPurchaseOrderByUserId(int userId, string sortBy, int firstItem, int pageSize)
        {
            ECommerce.DAL.CustomCollection<TblUserOrder> userOrders = TblUserOrder.GetTblUserOrderByUserId(userId);
            ECommerce.DAL.CustomCollection<TblProductPurchaseOrder> tblProductPurchaseOrder = new ECommerce.DAL.CustomCollection<TblProductPurchaseOrder>();
            if (userOrders != null && userOrders.Count > 0)
            {
                string list = string.Empty;
                for (int i = 0; i < userOrders.Count; i++)
                {
                    TblUserOrder userOrder = userOrders[i];
                    list += i == 0 ? "( " : "";
                    list += userOrder.OrderId.ToString();
                    list += i == userOrders.Count - 1 ? ")" : ",";
                }
                if (!string.IsNullOrEmpty(list))
                {
                    string queryCondition = " OrderId IN " + list;
                    tblProductPurchaseOrder = TblProductPurchaseOrderExtended.GetBackendProductPurchaseOrderListByCondition(queryCondition, sortBy, firstItem, pageSize);
                }
            }

            return LoadFromCustomCollection(tblProductPurchaseOrder);
        }
        public static int GetProductPurchaseOrderByUserIdCount(int userId)
        {
            int count = 0;
            ECommerce.DAL.CustomCollection<TblUserOrder> userOrders = TblUserOrder.GetTblUserOrderByUserId(userId);
            ECommerce.DAL.CustomCollection<TblProductPurchaseOrder> tblProductPurchaseOrder = new ECommerce.DAL.CustomCollection<TblProductPurchaseOrder>();
            if (userOrders != null && userOrders.Count > 0)
            {
                string list = string.Empty;
                for (int i = 0; i < userOrders.Count; i++)
                {
                    TblUserOrder userOrder = userOrders[i];
                    list += i == 0 ? "( " : "";
                    list += userOrder.OrderId.ToString();
                    list += i == userOrders.Count - 1 ? ")" : ",";
                }
                if (!string.IsNullOrEmpty(list))
                {
                    string queryCondition = " OrderId IN " + list;
                    count = TblProductPurchaseOrderExtended.GetBackendProductPurchaseOrderListByConditionCount(queryCondition);
                }
            }

            return count;
        }

        public static ReadWriteCustomCollection<ProductPurchaseOrder> GetBackendProductPurchaseOrderListByCondition(string queryCondition, string sortBy, int firstItem, int pageSize)
        {
            ECommerce.DAL.CustomCollection<TblProductPurchaseOrder> tblProductPurchaseOrder = TblProductPurchaseOrderExtended.GetBackendProductPurchaseOrderListByCondition(queryCondition, sortBy, firstItem, pageSize);
            return LoadFromCustomCollection(tblProductPurchaseOrder);
        }

        public static int GetBackendProductPurchaseOrderListByConditionCount(string queryCondition)
        {
            int count = TblProductPurchaseOrderExtended.GetBackendProductPurchaseOrderListByConditionCount(queryCondition);
            return count;
        }
        public static ReadWriteCustomCollection<ProductPurchaseOrder> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblProductPurchaseOrder> tblProductPurchaseOrder)
        {
            ReadWriteCustomCollection<ProductPurchaseOrder> productPurchaseOrder = new ReadWriteCustomCollection<ProductPurchaseOrder>();
            for (int i = 0; i < tblProductPurchaseOrder.Count; i++)
            {
                productPurchaseOrder.Add(new ProductPurchaseOrder(tblProductPurchaseOrder[i]));
            }
            return productPurchaseOrder;
        }

       

    }
}
